package wdrun

import (
	"os"
	"strings"
)

type Args struct {
	desc string
	kv   map[string]string
	cmd  map[string]*Args
}

func (a *Args) GetInfo() (string, map[string]string, map[string]*Args) {
	return a.desc, a.kv, a.cmd
}

func ParseArgs() *Args {
	args := os.Args
	a := &Args{
		desc: args[0],
		kv:   make(map[string]string),
		cmd:  make(map[string]*Args),
	}
	cmd := "-"
	for i := 1; i < len(args); i++ {
		if args[i][0] == '-' {
			if cmd == "-" {
				if i+1 < len(args) && args[i+1][0] != '-' {
					a.kv[strings.TrimLeft(args[i], "-")] = args[i+1]
					i++
				} else {
					a.kv[strings.TrimLeft(args[i], "-")] = "nil"
				}
			} else {
				if i+1 < len(args) && args[i+1][0] != '-' {
					a.cmd[cmd].kv[strings.TrimLeft(args[i], "-")] = args[i+1]
					i++
				} else {
					a.cmd[cmd].kv[strings.TrimLeft(args[i], "-")] = "nil"
				}
			}
		} else {
			cmd = args[i]
			aa := &Args{
				desc: args[i],
				kv:   map[string]string{},
			}
			a.cmd[cmd] = aa
		}
	}
	return a
}
